package com.wxy.gxs.springbootprojectday21.web.controller;

import com.alibaba.fastjson.JSON;
import com.wxy.gxs.springbootprojectday21.bean.RequestAccessTockenParam;
import com.wxy.gxs.springbootprojectday21.untils.DownloadUtil;
import com.wxy.gxs.springbootprojectday21.untils.HttpHelper;
import com.wxy.gxs.springbootprojectday21.untils.HttpUtil02;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;

@Controller
public class UserController {
    @Autowired
    private DownloadUtil downloadUtil;

    @Autowired
    private HttpHelper httpHelper;



    /**
     * 总的思路:
     *  1.获取到需要将文件上传到什么地方,
     *  需要注意的就是要将路径的斜线替换掉
     *  2.在通过文件的路径,加上文件的名称创建文件
     *  3.最后,在通过file的形式,tranfor转移到自己创建的文件路径下面
     */


    /**
     *
     * @param request
     * @return
     * @throws FileNotFoundException
     *
     * 实验目的:
     *  1.通过springboot实现文件的上传下载操作
     * 要求:
     *  1.需要在项目工程下面
     *
     *
     */
    @RequestMapping("/method01")
    @ResponseBody
    public Object method01(HttpServletRequest request,MultipartFile file) throws IOException {


        String relativelyPath=System.getProperty("user.dir");

        String path = relativelyPath.replace("\\", "\\\\");
        File file2 = new File(path,"upload02");
        String path1 = file2.getPath();
        System.out.println("这是路径测试"+path1);
        if(!file2.exists()){
            file2.mkdir();

        }



        File file1 = new File(path1, file.getOriginalFilename());
        file.transferTo(file1);


        return "文件上传成功!!";




    }

    @RequestMapping("/")
    public  String toIndex(){

        return "index";

    }
    @RequestMapping("/download/{fileName}")
    public void download(@PathVariable("fileName") String fileName , HttpServletResponse response){


//        通过system.getProperty(user.dir)获取到项目的决定路径
        String relativelyPath=System.getProperty("user.dir");
//        将路径里面的内容替换掉\\
        String path = relativelyPath.replace("\\", "\\\\");
//        new File()的第二种巧妙的使用 左边的话就是路径,右边的话就是该路径下面是否存在该文件,如果不存在的话
//        就可以在该路径下面创建文件
        File file2 = new File(path,"upload02\\\\"+fileName);
//        通过下载工具类,调用方法,第一个参数文件所在的位置,第二个参数文件的名称,第三个参数,响应的响应,
//        通过浏览器下载内容其实就是通过repsonse的形式相应出来,
/**
 * 总的思路:
 *  1.获取到需要下载的文件的路径
 *  2.通过工具类下载
 *  巧妙的地方:
 *      1.new File() 里面有两个参数,
 *
 *
 */
        downloadUtil.download(file2, fileName, response, false);




    }

    @RequestMapping("/user/login")
    public String method04(String code){
        System.out.println("这是测试的数据"+code);

        RequestAccessTockenParam requestAccessTockenParam = new RequestAccessTockenParam();
        System.out.println("=======================");
        System.out.println(requestAccessTockenParam);

        requestAccessTockenParam.setClient_id("4282184032");
        requestAccessTockenParam.setClient_secret("af04dc8a4469640d0b36de5abc2773c3");
        requestAccessTockenParam.setCode(code);
        String requestParam = JSON.toJSONString(requestAccessTockenParam);
        System.out.println("这是请求的参数 ::   "+requestParam);
                    //https://api.weibo.com/oauth2/access_token?
        String url="https://api.weibo.com/oauth2/access_token";
        StringBuilder sb = new StringBuilder();
        sb.append("grant_type=authorization_code");
        sb.append("&client_id="+requestAccessTockenParam.getClient_id());
        sb.append("&client_secret=" + requestAccessTockenParam.getClient_secret());
        sb.append("&redirect_uri=" + requestAccessTockenParam.getRedirect_uri());
        sb.append("&code=" + code);
        String str="client_id=4282184032&client_secret=af04dc8a4469640d0b36de5abc2773c3&grant_type=authorization_code&redirect_uri=wxyblog.5gzvip.idcfengye.com/user/login&code="+code;

        System.out.println("========================");
        System.out.println(sb.toString());

        String content = HttpUtil02.sendPost(url, sb.toString());
        Map map = JSON.parseObject(content, Map.class);
        String access_token = (String) map.get("access_token");
        String uid = (String) map.get("uid");

        System.out.println("这是获取到的令牌::  "+content);
        System.out.println("这是截取到的"+access_token);
        String url2="“https://api.weibo.com/2/users/show.json?uid="+uid+"&access_token="+access_token;
//        String s = HttpUtil02.sendGet(url2);
        String s = httpHelper.Get(url2);

        System.out.println("----------最后的结果-------------");
        System.out.println(s);


        return "index02";





    }
    @RequestMapping("/user/toIndex")
    public String method05(){

        return "toIndex";

    }


}
